package xyz.yuzegod.megawalls.classes.normal.blaze;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.event.PlayerKillEvent;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Blaze extends Classes
{
    public static Map<GamePlayer, Integer> skill3Cooldown;
    
    public Blaze() {
        super("Blaze", "烈焰人", ChatColor.RED, Material.BLAZE_POWDER, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.REMOTE, ClassesInfo.Orientation.CONTROL }, ClassesInfo.Difficulty.TWO);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDk0Mzc2OTkxODIsInByb2ZpbGVJZCI6ImE5MGI4MmIwNzE4NTQ0ZjU5YmE1MTZkMGY2Nzk2NDkwIiwicHJvZmlsZU5hbWUiOiJJbUZhdFRCSCIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjVhNTQ4NmZmNjMyYTEyNTJhNjM5ZjFlZmZiYzhiNjZjYzMzMzNhOTQ2NDY0OGUzYzljY2I5MWQ4YzE0MzM3NiJ9fX0=", "ZOZnYDhaLvhdesAjSgzGH2jmI+7aPCcRYVKHRQvrVKR9WA6hwnTqCIKdATwSe5N7VIvk26SBAs/6CtzV4NdF6QDrbGtWDlMxCqrTQ9aIeQdeCezO+W5RGRlQAHVPAIl6iKYXtIEDOhT+mKJt3QNVoXDEGdk2AwKe7FYGH9SGaqVfOid/LtUr1+StQG7MKKemMOhsH6Lqe9U1ym5B0ADQQul+RN2kvX4MrtUsAd3wPrDHFhS8lwHONSUH9gojseFZ7wQj9hCRIhjtgUqyr20u7USKFScRR5PIbJgvGRk8oOtdoDiaLItOgqcljZGs24M4S8wN013zpBb4WkZwGKPBQdhFTJBYa4KfiE0OP1G5EgxQWgxtFF4fYPPMztLzx9ZI9jKBcsG3NPzVG+mKGs57mxC6H93yhmKofVKsWo1q7fU1kWc7yL7m2bM4xjAPPmXgJc9BmhUNEKcEbfa5IxTFjarKVZ/Y79ys57/jmysZ4CElVZexWM8iOeIb4mfEzVcN7tfsSCh0CYSimjW7XBupF+VqR2xPILn/WlaYGALFXSkA+2gvBiCTTlts44gnJ7oDdoMcu9WI3ATFGQSjWX5D/pPwdxwiKBrLUuKDkWfHT6754gVqtF8XqZqpjBda+lAhosxnl8w3IGLuvqydyisLdCMS8aSTbQ9rIpktBH0eMTQ=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FIRE, 1).build());
                        items.add(new ItemBuilder(Material.ARROW, 32).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FIRE, 1).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 1).build());
                        items.add(new ItemBuilder(Material.ARROW, 56).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 1).build());
                        items.add(new ItemBuilder(Material.ARROW, 64).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.ARROW, 16).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 2).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 2).build());
                        items.add(new ItemBuilder(Material.ARROW, 56).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 2).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 2).build());
                        items.add(new ItemBuilder(Material.ARROW, 56).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Blaze.this.nameColor + Blaze.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7弓");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 力量 I");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add("    §8▪ 火焰保护 I");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7箭 §8x32");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add("    §8▪ 火焰保护 I");
                        lore.add("    §a+ 弹射物保护 I");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7箭 §8x32 ➜ §ax56");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add("    §8▪ 火焰保护 I ➜ §aII");
                        lore.add("    §8▪ 弹射物保护 I");
                        lore.add(" §8▪ §7速度药水 §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7箭 §8x56 ➜ §ax64");
                        lore.add(" §8▪ §7箭 §8x56 ➜ §ax16");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7弓");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 力量 I ➜ §aII");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II");
                        lore.add("    §8▪ 火焰保护 II");
                        lore.add("    §8▪ 弹射物保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7箭 §8x64 ➜ §ax40");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II ➜ §aIII");
                        lore.add("    §8▪ 火焰保护 II");
                        lore.add("    §8▪ 弹射物保护 II");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你的献祭之殇高于一切.");
        lore.add("§7你喜欢燃着的东西,就比如,敌人.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 15000;
    }
    
    @Override
    public int energyMelee() {
        return 8;
    }
    
    @Override
    public int energyBow() {
        return 4;
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> {
            if (Blaze.skill3Cooldown.getOrDefault(gamePlayer, 0) > 0) {
                Blaze.skill3Cooldown.put(gamePlayer, Blaze.skill3Cooldown.get(gamePlayer) - 1);
            }
            gamePlayer.sendActionBar(this.getSkillTip(gamePlayer));
        });
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer) + " " + this.thirdSkill.getSkillTip(gamePlayer);
    }
    
    @EventHandler
    public void onPlayerKill(final PlayerKillEvent e) {
        final GamePlayer killer = e.getKiller();
        final Classes classes = ClassesManager.getSelected(killer);
        if (classes.equals(this)) {
            this.secondSkill.use(killer, killer.getPlayerStats().getKitStats(classes));
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (e.isCancelled()) {
            return;
        }
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            final GamePlayer damager = GamePlayer.get(e.getDamager().getUniqueId());
            if (damager.isSpectator() || damager.getGameTeam().isInTeam(GamePlayer.get(e.getEntity().getUniqueId()))) {
                return;
            }
            final Classes classes = ClassesManager.getSelected(damager);
            if (classes.equals(this)) {
                final ThirdSkill thirdSkill = (ThirdSkill)this.getThirdSkill();
                final int level = thirdSkill.getPlayerLevel(damager);
                if (MegaWalls.getRandom().nextInt(100) <= thirdSkill.getAttribute(level) * 100.0 && Blaze.skill3Cooldown.getOrDefault(damager, 0) == 0) {
                    final Player player = damager.getPlayer();
                    if (player.hasPotionEffect(PotionEffectType.REGENERATION)) {
                        player.removePotionEffect(PotionEffectType.REGENERATION);
                    }
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 60, 0));
                    e.getEntity().setFireTicks(60);
                    Blaze.skill3Cooldown.put(damager, 1);
                }
            }
        }
        else if (e.getEntity() instanceof Player && e.getDamager() instanceof Arrow) {
            final GamePlayer player2 = GamePlayer.get(((Player)((Arrow)e.getDamager()).getShooter()).getUniqueId());
            final Classes classes = ClassesManager.getSelected(player2);
            if (classes.equals(this)) {
                final ThirdSkill thirdSkill = (ThirdSkill)this.getThirdSkill();
                final int level = thirdSkill.getPlayerLevel(player2);
                if (MegaWalls.getRandom().nextInt(100) <= thirdSkill.getAttribute(level) * 200.0 && Blaze.skill3Cooldown.getOrDefault(player2, 0) == 0) {
                    final Player shooter = player2.getPlayer();
                    if (shooter.hasPotionEffect(PotionEffectType.REGENERATION)) {
                        shooter.removePotionEffect(PotionEffectType.REGENERATION);
                    }
                    shooter.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 60, 0));
                    e.getEntity().setFireTicks(60);
                    Blaze.skill3Cooldown.put(player2, 1);
                }
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onProjectileLaunch(final ProjectileLaunchEvent e) {
        if (e.isCancelled()) {
            return;
        }
        if (e.getEntity().getShooter() instanceof Player) {
            final GamePlayer shooter = GamePlayer.get(((Player)e.getEntity().getShooter()).getUniqueId());
            final Classes classes = ClassesManager.getSelected(shooter);
            if (classes.equals(this) && e.getEntity().getType() == EntityType.ARROW) {
                e.getEntity().setMetadata("Arrow", (MetadataValue)MegaWalls.getFixedMetadataValue());
            }
        }
    }
    
    static {
        Blaze.skill3Cooldown = new HashMap<GamePlayer, Integer>();
    }
}
